/**
 * @author Stephan Pfaff (http://www.derdaumen.net)
 */

var libPathPHP = '../lib/php/';
var libPathJS = '../lib/js/';
var noImage = "../css/backend/screen/images/noImage.png";
var imageRoot = 'images/';
var url = location.href;
var urlTemp = url.substring(0, url.lastIndexOf('/'));
var siteRoot = url.substring(0, urlTemp.lastIndexOf('/')+1);

var urlLoad = libPathPHP+'load.php';
var urlSave = libPathPHP+'save.php';
var urlNew = libPathPHP+'new.php';
var urlDelete = libPathPHP+'delete.php';
var urlDeleteThumb = libPathPHP+'deleteThumb.php';
var urlDeleteImage = libPathPHP+'deleteImage.php';

var debug = true;
var categories = null;
var projects = null;
var buttons = null;

var imgwidth = 75;
var imgheight = 75;
var tempImagePath = '';

var maxImgSize = '5MB';

var cropDialog = null;

function getCategories(){
	if(categories == null) {
		categories = $('#select_category');
		var categoryContainer = new ItemContainer('category',
				'ctg',
				urlLoad,
				urlSave,
				urlNew,
				urlDelete,
				urlDeleteThumb,
				null);
		$.extend(categories, categoryContainer);
	}
	return categories;
};

function getProjects(){
	if(projects == null) {
		projects = $('#select_project');
		var projectContainer = new ItemContainer('project',
				'prj',
				urlLoad,
				urlSave,
				urlNew,
				urlDelete,
				urlDeleteThumb,
				null);
		$.extend(projects, projectContainer);
	}
	return projects;
};

function getButtons(){
	if(buttons == null) {
		buttons = $('#select_button');
		var buttonContainer = new ItemContainer('button',
				'btn',
				urlLoad,
				urlSave,
				urlNew,
				urlDelete,
				urlDeleteThumb,
				urlDeleteImage);
		$.extend(buttons, buttonContainer);
	}
	return buttons;
};

function focus(container) {
	switch(container.type) {
		case getCategories.type:
			getProjects.clearFormElements(true);
			getButtons.clearFormElements(true);
			break;
		case getProjects.type:
			getButtons.clearFormElements(true);
			break;
		default:
			break;
	}
}

/*
 * ############################################################################
 * ################################## Main ####################################
 * ############################################################################
 */

function init() {
	$('.yform').clearForm();
	getCategories();
	getProjects();
	getButtons();
	getCategories().load({prefix: getCategories().prefix}, null, true);
	
	/*
	 * #########################################################################
	 * ############################# Category Buttons ##########################
	 * #########################################################################
	 */
	
	$('#select_category').focus( function() {
		focus(getCategories());
		getCategories().change();
	});
	$('#select_category').change( function() {
		getCategories().change();
	});
	$('#btn_saveCtg').click( function() {
		this.blur();
		getCategories().saveItem();
	});

	// New Category Dialog			
	$('#dialog_newCtg').dialog({
		autoOpen: false,
		width: 250,
		modal: true,
		position: [$('#btn_newCtg').offset().left-40, $('#btn_newCtg').offset().top+50],
		buttons: {
			"Ok": function() {
				if (getCategories().checkAlias($('#ctg_new_alias').val())) {
					$('#ctg_alias').val($('#ctg_new_alias').val());
					$('#ctg_new_alias').val('');
					getCategories().newItem();
					$(this).dialog("close");
				}
			}, 
			"Cancel": function() {
				$(this).dialog("close");
			} 
		}
	});
	
	$('#btn_newCtg').click( function() {
		this.blur();
		getCategories().clearFormElements(false);
		$('#dialog_newCtg').dialog('open');
	});
	$('#btn_resetCtg').click( function() {
		this.blur();
		$('#select_category').focus().change();
	});
	$('#btn_deleteCtg').click( function() {
		this.blur();
		getCategories().deleteItem();
	});
	$('#ctg_thumb_delete_btn').click( function() {
		this.blur();
		getCategories().deleteThumb();
	});
	
	/*
	 * #########################################################################
	 * ############################# Project Buttons ###########################
	 * #########################################################################
	 */
	
	$('#select_project').focus( function() {
		focus(getProjects());
		getProjects().change();
	});
	$('#select_project').change( function() {
		getProjects().change();
	});
	$('#btn_savePrj').click( function() {
		this.blur();
		getProjects().saveItem();
	});
	// New Project Dialog			
	$('#dialog_newPrj').dialog({
		autoOpen: false,
		width: 250,
		modal: true,
		position: [$('#btn_newPrj').offset().left-40, $('#btn_newPrj').offset().top+50],
		buttons: {
			"Ok": function() {
				if (getProjects().checkAlias($('#prj_new_alias').val())) {
					$('#prj_alias').val($('#prj_new_alias').val());
					$('#prj_new_alias').val('');
					getProjects().newItem();
					$(this).dialog("close");
				}
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	
	$('#btn_newPrj').click( function() {
		this.blur();
		getProjects().clearFormElements(false);
		$('#dialog_newPrj').dialog('open');
	});
	$('#btn_resetPrj').click( function() {
		this.blur();
		$('#select_project').focus().change();
	});
	$('#btn_deletePrj').click( function() {
		this.blur();
		getProjects().deleteItem();
	});
	$('#prj_thumb_delete_btn').click( function() {
		this.blur();
		getProjects().deleteThumb();
	});
	$('#prj_thumb_container').click( function() {
		getProjects().showCropDialog();
	});
	
	/*
	 * #########################################################################
	 * ############################# Button Buttons ############################
	 * #########################################################################
	 */
	
	$('#select_button').focus( function() {
		focus(getButtons());
		getButtons().change();
	});
	$('#select_button').change( function() {
		getButtons().change();
	});
	$('#btn_saveBtn').click( function() {
		this.blur();
		getButtons().saveItem();
	});
	// New Button Dialog			
	$('#dialog_newBtn').dialog({
		autoOpen: false,
		width: 250,
		modal: true,
		position: [$('#btn_newBtn').offset().left-40, $('#btn_newBtn').offset().top+50],
		buttons: {
			"Ok": function() {
				if (getButtons().checkAlias($('#btn_new_alias').val())) {
					$('#btn_alias').val($('#btn_new_alias').val());
					$('#btn_new_alias').val('');
					getButtons().newItem();
					$(this).dialog("close");
				}
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	
	$('#btn_newBtn').click( function() {
		this.blur();
		getButtons().clearFormElements(false);
		$('#dialog_newBtn').dialog('open');
	});
	$('#btn_resetBtn').click( function() {
		this.blur();
		$('#select_button').focus().change();
	});
	$('#btn_deleteBtn').click( function() {
		this.blur();
		getButtons().deleteItem();
	});
	$('#btn_thumb_delete_btn').click( function() {
		this.blur();
		getButtons().deleteThumb();
	});
	$('#btn_img_delete_btn').click( function() {
		this.blur();
		getButtons().deleteImage();
	});
	$('#btn_x').change( function() {
		getButtons().updatePreview();
	});
	$('#btn_y').change( function() {
		getButtons().updatePreview();
	});
	$('#btn_content_x').change( function() {
		getButtons().updatePreview();
	});
	$('#btn_content_y').change( function() {
		getButtons().updatePreview();
	});
	$('#btn_content_width').change( function() {
		getButtons().updatePreview();
	});
	$('#btn_content_height').change( function() {
		getButtons().updatePreview();
	});
	
	/*
	 * #########################################################################
	 * ############################# Dialogs / Utils ###########################
	 * #########################################################################
	 */
	
	// Crop Dialog			
	cropDialog = $('#dialog_crop').dialog({
		autoOpen: false,
		width: 950,
		height: 610,
		modal: true,
		open: function(event, ui) {
		},
		close: function(event, ui) {
			$('.swfupload').show();
		},
		buttons: {
			"Ok": function() {
				$(this).dialog("close");
				if(cropDialog.callback == 'saveThumb') {
					cropDialog.container.getInstance().saveThumb();
				} else if(cropDialog.callback == 'savePreview') {
					cropDialog.container.getInstance().savePreview();
				} else {
					
				}
			}, 
			"Cancel": function() {
				resetCrop();
				$(this).dialog("close");
			} 
		}
	});
	cropDialog.container = null;
	
};